package com.koocloud.electroplatemanage.controller;

import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.pojo.AssignJobVo;
import com.koocloud.electroplatemanage.pojo.DrawNoVo;
import com.koocloud.electroplatemanage.service.DrawNoService;

import com.koocloud.electroplatemanage.service.IncomePartUnitService;
import com.koocloud.electroplatemanage.service.TypeWorkService;
import com.koocloud.electroplatemanage.utils.DeleteFileUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;


/**
 * @program: electroplatemanage
 * @description: 图号控制类
 * @author: zww
 * @create: 2020-09-29 16:23
 */
@RestController
@RequestMapping("/drawno")
public class DrawNoController {
    static Logger log = Logger.getLogger("drawno");
    @Autowired
    DrawNoService drawNoService;
    @Autowired
    IncomePartUnitService incomePartUnitService;
    @Autowired
    TypeWorkService  typeWorkService;

    /**
     * @Description:通过pk查询图号表一条记录
     * @Param: [pk]
     * @return: com.koocloud.electroplatemanage.common.ResponseTemplate
     * @Author: zww
     * @date: 2020/9/30
     */
    @RequestMapping(value = "/selectByPk", method = RequestMethod.POST, name = "主键查询")
    public ResponseTemplate selectByPk(String pkDrawNo) {
        //通过pk查询图号表的一条记录
        return drawNoService.selectByPk(pkDrawNo);
    }

    /**
     * @Description: 查询所有记录 分页  来件单位(参照来件单位表) 零件名称
     * @Param: [page,limit  ,name,partName]
     * @return: com.koocloud.electroplatemanage.common.ResponseTemplate
     * @Author: zww
     * @date: 2020/9/30
     */
    @RequestMapping(value = "/selectAllData", method = RequestMethod.POST, name = "全表查询")
    public ResponseTemplate selectAllData(String page, String limit,DrawNoVo drawNoVo) {
        //分页参数为空则赋值0
            if (!StringUtils.isNotEmpty(page)) {
                page = "0";
            }
            if (!StringUtils.isNotEmpty(limit)) {
                limit = "0";
            }
        //查询所有记录 分页
        return drawNoService.selectAllData(page,limit,drawNoVo);
    }

    /**
     *@Description: 校验填写的图号表主键
     *@Param: [drawNoVo]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/16
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST, name = "校验填写的图号表主键")
    public ResponseTemplate check(@RequestBody DrawNoVo drawNoVo) {
        //校验填写的分配工作表主键
        return drawNoService.check(drawNoVo);

    }
    /**
     *@Description:插入一条记录
     *@Param: [drawNoVo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/30
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, name = "新增一条数据")
    public ResponseTemplate insert(@RequestBody DrawNoVo drawNoVo) {
        //插入一条记录
        return drawNoService.insert(drawNoVo);
    }
    /**
     *@Description:批量插入记录
     *@Param: [listDrawNoVo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/30
     */
    @RequestMapping(value = "/batchInsert", method = RequestMethod.POST, name = "批量新增数据")
    public ResponseTemplate batchInsert(@RequestBody List<DrawNoVo> listDrawNoVo) {
        //批量插入记录
        return drawNoService.batchInsert(listDrawNoVo);
    }
    /**
     *@Description:逻辑删除一条记录
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/30
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, name = "逻辑删除一条记录")
    public ResponseTemplate delete(@RequestParam(name="drawNoCode") String pk,@RequestParam(name="deleteFlag") String deleteFlag) {
        //逻辑删除一条记录
        return drawNoService.delete(pk,deleteFlag);
    }
    /**
     *@Description:通过pks批量逻辑删除记录
     *@Param: [listPks]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/30
     */
    @RequestMapping(value = "/batchDeleteByPks", method = RequestMethod.POST, name = "批量逻辑删除记录")
    public ResponseTemplate batchDeleteByPks(@RequestBody List<String> listPks) {
        //通过pks批量逻辑删除记录
        return drawNoService.batchDeleteByPks(listPks);
    }
    /**
     *@Description:修改一条记录
     *@Param: [drawNoVo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/30
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, name = "修改一条记录")
    public ResponseTemplate update(@RequestBody DrawNoVo drawNoVo) {
        //修改一条记录
        return drawNoService.update(drawNoVo);
    }


    /**
     *@Description: 获取现有的所有来件单位
     *@Param: []
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/19
     */
    @RequestMapping(value = "/selectUnit", method = RequestMethod.POST, name = "获取现有的所有来件单位")
    public ResponseTemplate selectUnit(String idOrName){
        return incomePartUnitService.selectUnit(idOrName);
    }


    /**
     *@Description: 获取现有的所有工种
     *@Param: []
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/10/19
     */
    @RequestMapping(value = "/selectTypeWork", method = RequestMethod.POST, name = "获取现有的所有工种")
    public ResponseTemplate selectTypeWork(String idOrName){
        return typeWorkService.selectTypeWork(idOrName);
    }


    /**
     *@Description: 图号基础情报excel导入
     *@Param: [request, files]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/17
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST, name = "图号基础情报excel导入")
    public ResponseTemplate importExcel(HttpServletRequest request, @RequestParam("file") MultipartFile files){
        //获取上传时的文件名称
        String fileName = files.getOriginalFilename();
        //获取程序当前路径
        String path = System.getProperty("user.dir");
        try {
            //获取上传文件的字节数组
            byte[] bytes = files.getBytes();
            //创建存储位置
            BufferedOutputStream  stream = new BufferedOutputStream(new FileOutputStream(new File(path+ File.separator + fileName)));
            //写入
            stream.write(bytes);
            //流关闭
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //存储文件的本地位置
        String file = path + File.separator +fileName;
        //获取上传人id,即创建人id
        String id  = request.getHeader("userId");

        return drawNoService.importExcel(id,file);

    }
}